<?php
/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 *
 * 
 */
class UserIdentity extends CUserIdentity
{
	private $_id;
	const ACCOUNT_NOT_ACTIVE = 666; 

	public function authenticate()
    {
		$record = User::model()->findByAttributes(array('username'=>$this->username));

        if( $record===null || $record->password !== self::hash($this->password) ) {
            $this->errorCode=self::ERROR_USERNAME_INVALID;

        } else {
            $this->_id = $record->id;

            $this->errorCode=self::ERROR_NONE;
        }

        return !$this->errorCode;
    }

    public function getId()
    {
        return $this->_id;
    }


    public function hash($password)
    {
        return base64_encode(pack('H*', sha1((trim($password)))));
    } 

}